﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ITAP.ContourBuilder.Components.Dimensions;
using System.Windows.Forms;

using ITAP.ContourBuilder.Components;
using ITAP.Core.Modelling;


namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandAddElementLeaderDimension : SerializedCommand
    {
        public CommandAddElementLeaderDimension(ContourControl control)
            : base(control)
        {
        }

        private PointF prevMousePoint;
        private ElementLeaderDimension dimension = null;
        private bool isMoveMovedInFirstTime = true;
        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            // Выбираем элемент для создания размера
            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == MouseButtons.Left)
            {
                IEnumerable<IContourComponent> allComponents = this.control.ContourContainer.GetAllInnerComponents();
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                ContourElement element = null;
                foreach (IContourComponent component in allComponents)
                    if (component is ContourElement && component.CanSelectByPoint(point))
                        element = component as ContourElement;

                if (element == null)
                {
                    this.Cancel();
                    return;
                }

                dimension = (ElementLeaderDimension)DimensionFactory.CreateLeaderDimension(element, GlobalData.DimensionTextFont, "");
                element.GetRootModel().AddInnerComponent(dimension);
                this.control.ContourContainer.Update();
                prevMousePoint = state.MouseState.State.Location;
            }
            else if (state.MouseState.MouseMode == MouseMode.Move && state.MouseState.State.Button == MouseButtons.Left
                && dimension != null)
            {
                ModelPoint prevPoint = this.ScreenPointToModelPoint(prevMousePoint);
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                ModelVector vector = new ModelVector(prevPoint, point);

                if (isMoveMovedInFirstTime && vector.Length < 10)
                    return;

                if (isMoveMovedInFirstTime)
                {
                    if (Math.Abs(vector.X) > Math.Abs(vector.Y))
                    {
                        dimension.LeaderType = LeaderDimensionType.Horizontal;
                        dimension.PrefixSymbol = "Y";
                    }
                    else
                    {
                        dimension.LeaderType = LeaderDimensionType.Vertical;
                        dimension.PrefixSymbol = "X";
                    }
                    isMoveMovedInFirstTime = false;
                }

                if (dimension.LeaderType == LeaderDimensionType.Vertical)
                    dimension.OffsetFromPrimitive += vector.Y;
                else
                    dimension.OffsetFromPrimitive += vector.X;

                prevMousePoint = state.MouseState.State.Location;
                dimension.Recalculate();
                this.control.Invalidate();
            }
            else if (state.MouseState.MouseMode == MouseMode.Up && state.MouseState.State.Button == MouseButtons.Left)
            {
                this.control.UpdateModel();
                this.Exit();
                return;
            }
        }

        public override string ToString()
        {
            return "Создать выноску для элемента";
        }
    }
}
